<?php
class Chong_User
{
    const _USERNAME_ = 'chong_username';
    const _AVATAR_URL_ = 'chong_avatar_url';
    const _LAST_LOGIN_TIME_ = 'chong_last_login_time';
    const _NICK_NAME_ = 'chong_nick_name';
    
   public function __construct()
   {
       //$this->login('zhwphp');
   }
    
    public function login($username, $avatar_url, $last_login_time, $nickname)
    {
        Cookie::set(self::_USERNAME_, $username);
        Cookie::set(self::_LAST_LOGIN_TIME_, $last_login_time);
        Cookie::set(self::_NICK_NAME_, $nickname);
        if ( !empty($avatar_url) ) {
            Cookie::set(self::_AVATAR_URL_, $avatar_url);
        } else {
            Cookie::set(self::_AVATAR_URL_, 'http://static.yingyonghui.com/icon/128/9999.png');
        }
    }
    
    public function logout()
    {
        Cookie::delete(self::_USERNAME_);
    }
    
    public function is_login()
    {
        return (bool) $this->username();
    }
    
    public function username()
    {
        return Cookie::get(self::_USERNAME_);
    }
    
    public function get_user_avatar() {
        return Cookie::get(self::_AVATAR_URL_);
    }
    
    public function get_last_login_time() {
        return Cookie::get(self::_LAST_LOGIN_TIME_);
    }
    
    public function get_user_nick_name() {
        return Cookie::get(self::_NICK_NAME_);
    }
    /**
     * 应用豆余额数据
     *
     * @return int
     */
    public function balance()
    {        
        $balance = IAppPay_LiangXiang::balance($this->username());
        return ceil($balance / 10);
    }
}